#include ".\task_check_use_peek.h"

def_simple_fsm(task_check_use_peek,
               def_params(
                   byte_queue_t *ptQ;
                   bool bIsRequestDrop;
                   check_agent_t *ptTable;
                   int32_t nTableSize;
                   int32_t nCount;
               ))
end_def_simple_fsm(task_check_use_peek)

declare_fsm_implementation(task_check_use_peek)
declare_fsm_initialiser(task_check_use_peek,
                        args(
                            byte_queue_t *ptQueue,
                            check_agent_t *ptTable,
                            int32_t nTableSize
                        ))

fsm_initialiser(task_check_use_peek, args(byte_queue_t *ptQueue, check_agent_t *ptTable, int32_t nTableSize))
init_body(
    this.ptQ = ptQueue;
    this.ptTable = ptTable;
    this.nTableSize = nTableSize;
)

fsm_implementation(task_check_use_peek)
{
    def_states(CHECK_NUM, CALL_CHECK_STRING, CHECK_DROP)
    uint8_t chByte;
    bool bIsRequestDrop = false;

    body_begin();

        on_start({
            this.nCount = 0;
            this.bIsRequestDrop = true;
        })

        state(CHECK_NUM) {
            if (this.nCount < this.nTableSize) {
                update_state_to(CALL_CHECK_STRING);
            } else {
                this.nCount = 0;
                update_state_to(CHECK_DROP);
            }
        }

        state(CALL_CHECK_STRING) {
            if (fsm_rt_cpl == this.ptTable[this.nCount].fnCheck(&bIsRequestDrop)) {
                GET_ALL_PEEKED_BYTE(this.ptQ);
                LOG_RAW("GET_ALL_PEEKED_BYTE\r\n");
                fsm_cpl();
            } else {
                RESET_PEEK_BYTE(this.ptQ);
                this.bIsRequestDrop &= bIsRequestDrop;
                this.nCount++;
                transfer_to(CHECK_NUM);
            }
        }

        state(CHECK_DROP) {
            LOG_RAW("Check drop\n");
            if (this.bIsRequestDrop) {  // 字符不属于任何一个字串就被删除
                DEQUEUE_BYTE(this.ptQ, &chByte);
                LOG_RAW("Dequeue %c\n", chByte);
            } else {
                this.bIsRequestDrop = true;
            }
            transfer_to(CHECK_NUM);
        }

    body_end()
}
